package com.repair.order.utils;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.*;

/**
 * @author chunyu
 * @since 2024/10/3,12:56
 * 抢单池的线程池
 */
@Configuration
@EnableAsync
public class GrabOrderAsyncConfig {

    @Bean("grabOrderPoolExecutor")
    public Executor grabOrderPoolExecutor() {

        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("grabOrderPoolExecutor-%d").build();

        ExecutorService executor = new ThreadPoolExecutor(
                4,               // 设置核心线程数
                8,                          // 设置最大线程数
                0L,                         // 设置线程活跃时间
                TimeUnit.MINUTES,           // 时间单位秒
                new LinkedBlockingQueue<>(1024), // 设置队列容量
                namedThreadFactory,             // 设置线程名称前缀
                new ThreadPoolExecutor.CallerRunsPolicy());   // 设置拒绝策略（这里使用的是调用者运行策略）
        return executor;
    }

}
